package bupt.party.online.dao.impl;

import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;

import bupt.party.online.dao.IPartyNewsDao;
import bupt.party.online.model.PartyNews;

public class PartyNewsDaoImpl implements IPartyNewsDao {

	private HibernateTemplate hibernateTemplate;
	
	
	

	public boolean saveOrUpdate(PartyNews bean) {
		// TODO Auto-generated method stub
		try{
			this.hibernateTemplate.saveOrUpdate(bean);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public boolean delete(PartyNews bean) {
		// TODO Auto-generated method stub
		try{
			this.hibernateTemplate.delete(bean);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public List<PartyNews> findBySql(String sql) {
		// TODO Auto-generated method stub
		try{
			return this.hibernateTemplate.find(sql);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public int getResultCountByTag(int tag) {
		// TODO Auto-generated method stub
		if(tag <= 0) return 0;
		
		int count = 0;
		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		
		try{
			String sql = "select count(*) from PartyNews p where p.tag=?";
			
			Query query = session.createQuery(sql);
			query.setParameter(0, tag);
			
			count = ((Long)query.uniqueResult()).intValue();
			
			return count;
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
		
		
		
	}

	@SuppressWarnings("unchecked")
	public List<PartyNews> getResultsByPage(int tag, int firstResult,
			int pageSize) {
		// TODO Auto-generated method stub
		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		try{
			
			if(tag <= 0) return null;
			
			String sql = "from PartyNews p where p.tag=? order by postTime desc";
			Query query = session.createQuery(sql);
			query.setParameter(0, tag);
			query.setFirstResult(firstResult);
			query.setMaxResults(pageSize);
			
			return query.list();
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}

	@SuppressWarnings("unchecked")
	public List<PartyNews> QueryByConditions(String queryString,
			Object[] values, int firstResult, int pageSize) {
		// TODO Auto-generated method stub
		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		try{
			
			queryString += " order by postTime desc";
			
			Query q = session.createQuery(queryString);
			
			if(values != null){
				for(int i=0;i<values.length;i++){
					q.setParameter(i, values[i]);
				}
			}
			
			q.setFirstResult(firstResult);
			
			q.setMaxResults(pageSize);
			
			return q.list();
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public int getResultCount(String countSql, Object[] queryValues) {
		// TODO Auto-generated method stub
		int count = 0;
		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		
		try{
			
			Query query = session.createQuery(countSql);
			
			if(queryValues != null){
				for(int i=0;i<queryValues.length;i++){
					query.setParameter(i, queryValues[i]);
				}
			}
			
			count = ((Long)query.uniqueResult()).intValue();
			
			return count;
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public boolean deleteNewsById(int id) {
		// TODO Auto-generated method stub
		if(id < 0) return false;
		
		try{
			PartyNews newsBean = hibernateTemplate.get(PartyNews.class, id);
			if(newsBean == null) return false;
			hibernateTemplate.delete(newsBean);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
	}
	
	@Override
	public List<PartyNews> findCollegeNews(String queryOnTitle,Set<Integer> branchIds,int firstResult,int maxResults) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(PartyNews.class);
		
		criteria.add(Restrictions.eq("tag", 3));
		if(queryOnTitle != null && queryOnTitle.trim().length() > 0) criteria.add(Restrictions.like("title", "%" + queryOnTitle + "%"));
		criteria.add(Restrictions.in("branchId", branchIds));
		criteria.addOrder(Order.desc("postTime"));
		
		return this.hibernateTemplate.findByCriteria(criteria, firstResult, maxResults);
	}

	@Override
	public int getCollegeNewsCount(String queryString, Set<Integer> branchIds) {
		// TODO Auto-generated method stub

		Criteria criteria = this.hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(PartyNews.class);
		
		criteria.add(Restrictions.eq("tag", 3));
		if(queryString != null && queryString.trim().length() > 0) criteria.add(Restrictions.like("title", "%" + queryString + "%"));
		criteria.add(Restrictions.in("branchId", branchIds));
		criteria.addOrder(Order.desc("postTime"));
		
		return Integer.parseInt(criteria.setProjection(Projections.rowCount()).uniqueResult().toString());
	}
}
